/*
 * @Author: EyeSklear 1814580085@qq.com
 * @Date: 2024-01-22 16:10:08
 * @LastEditors: EyeSklear 1814580085@qq.com
 * @LastEditTime: 2024-03-27 16:56:17
 * @FilePath: \Scene_Back\src\video\video.controller.ts
 * @Description:
 *
 * Copyright (c) 2024 by EyeSklear , All Rights Reserved.
 */
/*
 * @Author: EyeSklear 1814580085@qq.com
 * @Date: 2024-01-11 14:34:54
 * @LastEditors: EyeSklear 1814580085@qq.com
 * @LastEditTime: 2024-01-18 16:12:05
 * @FilePath: \Scene_Back\src\video\video.controller.ts
 * @Description:
 *
 * Copyright (c) 2024 by EyeSklear , All Rights Reserved.
 */
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Res,
  UseInterceptors,
  UploadedFile,
  HttpStatus,
  Req,
} from '@nestjs/common';
import { VideoService } from './video.service';
import { CreateVideoDto } from './dto/create-video.dto';
import { UpdateVideoDto } from './dto/update-video.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import { Request } from 'express';
import * as fs from 'fs';
import * as path from 'path';
@Controller('video')
export class VideoController {
  constructor(private readonly videoService: VideoService) {}

  @Post('createVideoItem')
  createVideoItem(
    @Body() createVideoDto: CreateVideoDto,
    @Req() request: Request,
  ) {
    const user = request.user;
    createVideoDto.creator = (user as any).email;
    return this.videoService.create(createVideoDto);
  }

  @Get('example')
  getExampleVideo(@Res() res: Response): void {
    const filename = 'nuomantoudan.mp4';
    const videoFilePath = path.resolve(
      process.env.FILE_PATH + '/video/' + filename,
    );

    if (!fs.existsSync(videoFilePath)) {
      console.error('MP4 file does not exist.');
      res.status(HttpStatus.NOT_FOUND).send('MP4 file not found');
    }

    const stat = fs.statSync(videoFilePath);
    const fileSize = stat.size;
    const range = res.req.headers.range;

    if (range) {
      const parts = range.replace(/bytes=/, '').split('-');
      const start = parseInt(parts[0], 10);
      const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;

      const chunksize = end - start + 1;
      const file = fs.createReadStream(videoFilePath, {
        start,
        end,
      });
      const head = {
        'Content-Range': `bytes ${start}-${end}/${fileSize}`,
        'Accept-Ranges': 'bytes',
        'Content-Length': chunksize,
        'Content-Type': 'video/mp4',
      };

      res.writeHead(206, head);
      file.pipe(res);
    } else {
      const head = {
        'Content-Length': fileSize,
        'Content-Type': 'video/mp4',
      };
      res.writeHead(200, head);
      fs.createReadStream(videoFilePath).pipe(res);
    }
  }

  @Get('findAll')
  findAll() {
    return this.videoService.findAll();
  }

  @Get('findOne/:id')
  findOne(@Param('id') id: string) {
    return this.videoService.findOne(id);
  }

  @Patch('updateVideoItem/:id')
  updateVideoItem(
    @Param('id') id: string,
    @Body() updateVideoDto: UpdateVideoDto,
  ) {
    return this.videoService.update(id, updateVideoDto);
  }

  @Delete('removeVideoItem/:id')
  removeVideoItem(@Param('id') id: string) {
    return this.videoService.remove(id);
  }
}
